/*eslint-disable*/
import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import Home from "@/views/Home.vue";
import HomeSwiper from "@/views/HomeSwiper.vue";
import Message from "@/views/Message.vue";
const ItemContent = () => import("@/views/ItemLayout.vue");
const DetailPage = () => import("@/views/DetailPage.vue");
const SP = () => import("@/views/components/sp/index.vue");
// const TaoHome = () => import("@/views/components/TaoComponents/index.vue");
Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    path: "/",
    name: "HomeSwiper",
    component: HomeSwiper,
    meta: {
      keepAlive: true, // 需要被缓存
    },
    children: [
      {
        path: "/",
        name: "Home",
        component: Home,
        meta: {
          keepAlive: true, // 需要被缓存
        },
      },
      {
        path: "/message",
        name: "message",
        component: Message,
        meta: {
          keepAlive: true, // 需要被缓存
        },
      },
    ],
  },
  {
    path: "/itemContent",
    name: "ItemContent",
    component: ItemContent,
    meta: {
      keepAlive: false, // 需要被缓存
    },
  },
  {
    path: "/detailPage",
    name: "DetailPage",
    component: DetailPage,
    meta: {
      keepAlive: false, // 需要被缓存
    },
  },
  {
    path: "/sp",
    name: "SP",
    component: SP,
    meta: {
      keepAlive: false, // 需要被缓存
    },
  },
  // {
  //   path: "/taohome",
  //   name: "TaoHome",
  //   component: TaoHome,
  // },
];

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location: any) {
  return (originalPush.call(this, location) as any).catch((err: any) => err);
};

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    // keep-alive 返回缓存页面后记录浏览位置
    if (savedPosition && to.meta.keepAlive) {
      return savedPosition;
    }
    // 异步滚动操作
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ x: 0, y: 1 });
      }, 0);
    });
  },
});

export default router;
